package org.hairball.graph;

import java.util.HashSet;
import java.util.Set;

public class MutableGraph implements HairballGraph {
    private Set<String> unconnectedNodes = new HashSet<String>();
    private Set<Edge> edges = new HashSet<Edge>();
    private Set<String> connectedNodes = new HashSet<String>();

    public void addNode(String nodeId) {
        unconnectedNodes.add(nodeId);
    }

    public void addEdge(String from, String to) {
        addEdge(from, to, "");
    }

    public void visitEdges(EdgeVisitor edgeVisitor) {
        for (Edge edge : edges) {
            edgeVisitor.visit(edge.fromNode, edge.toNode, edge.type);
        }
    }

    public void visitNodes(NodeVisitor nodeVisitor) {
        for (String unconnectedNode : unconnectedNodes) {
            nodeVisitor.visit(unconnectedNode, false);
        }

        for (String connectedNode : connectedNodes) {
            nodeVisitor.visit(connectedNode, true);
        }
    }

    public void addEdge(String from, String to, String edgeType) {
        unconnectedNodes.remove(from);
        unconnectedNodes.remove(to);
        connectedNodes.add(from);
        connectedNodes.add(to);
        edges.add(new Edge(from, to, edgeType));
    }

   
}
